package edu.ustb.sql.sleuth.controller;

import edu.ustb.sql.sleuth.entity.*;
import edu.ustb.sql.sleuth.service.ZipkinSpansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ShiZhe
 * @create 2020-10-17 9:01
 */
@RestController
@RequestMapping("/sleuth")
@RefreshScope //开启动态刷新
public class SleuthController {

    @Autowired
    private ZipkinSpansService zipkinSpansService;

//    @RequestMapping(value = "/zipkinspans/{trace_id}",method = RequestMethod.GET)
//    public List<ZipkinSpans> findByTrace_id(@PathVariable Long trace_id){
//        List<ZipkinSpans> zipkinSpansList = zipkinSpansService.findByTrace_id(trace_id);
//        return zipkinSpansList;
//    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(Long s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }
    @RequestMapping(value = "/zipkinspans/{trace_id}",method = RequestMethod.GET)
    public ZipkinSpans findById(@PathVariable Long trace_id){
        ZipkinSpans zipkinSpans = zipkinSpansService.findById(trace_id);
        String s = stampToDate(zipkinSpans.getStart_ts());
        zipkinSpans.setDate(s);
        return zipkinSpans;
    }

}
